#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys, datetime, time

from sicost.config import app_config
from xpy3lib.utils import db_utils as util


def main():
    p_argv = sys.argv
    profile = 'dev'  # p_argv[1].split("=", 1)[-1]

    config = app_config[profile]

    test_db_mpp(p_config=config)
    time.sleep(5)

    test_db_rds(p_config=config)
    time.sleep(5)

    test_db_dbprod7(p_config=config)
    time.sleep(5)

    return 0


def test_db_mpp(p_config=None):
    try:
        validation_query = 'select 1 from sysibm.sysdummy1'
        conn = util.getConnectionDb2(p_config.DB_HOST_MPP_DB2,
                                     p_config.DB_PORT_MPP_DB2,
                                     p_config.DB_DBNAME_MPP_DB2,
                                     p_config.DB_USER_MPP_DB2,
                                     p_config.DB_PASSWORD_MPP_DB2)
        ok = conn is not None
        print('is DB2 connected????--test_db_mpp>>'.format(ok))
        if conn:
            df = util.query(conn=conn, sql=validation_query)
            print(df)
            util.closeConnection(conn=conn)
    except Exception as e:
        print('DB2Exception in main.py')
        print(str(e))


def test_db_rds(p_config=None):
    try:
        validation_query = 'select 1 from sysibm.sysdummy1'
        conn = util.getConnectionDb2(p_config.DB_HOST_RDS_DB2,
                                     p_config.DB_PORT_RDS_DB2,
                                     p_config.DB_DBNAME_RDS_DB2,
                                     p_config.DB_USER_RDS_DB2,
                                     p_config.DB_PASSWORD_RDS_DB2)
        ok = conn is not None
        print('is DB2 connected????--test_db_rds>>'.format(ok))
        if conn:
            df = util.query(conn=conn, sql=validation_query)
            print(df)
            util.closeConnection(conn=conn)
    except Exception as e:
        print('DB2Exception in main.py')
        print(str(e))


def test_db_dbprod7(p_config=None):
    try:
        validation_query = 'select 1 from sysibm.sysdummy1'
        conn = util.getConnectionDb2(p_config.DB_HOST_DBPROD7_DB2,
                                     p_config.DB_PORT_DBPROD7_DB2,
                                     p_config.DB_DBNAME_DBPROD7_DB2,
                                     p_config.DB_USER_DBPROD7_DB2,
                                     p_config.DB_PASSWORD_DBPROD7_DB2)
        ok = conn is not None
        print('is DB2 connected????--test_db_dbprod7>>'.format(ok))
        if conn:
            df = util.query(conn=conn, sql=validation_query)
            print(df)
            util.closeConnection(conn=conn)
    except Exception as e:
        print('DB2Exception in main.py')
        print(str(e))


if __name__ == '__main__':
    start = datetime.datetime.now()

    status = main()

    elapsed = float((datetime.datetime.now() - start).seconds)
    print("Time Used 4 All ----->>>> %f seconds" % (elapsed))
